package com.hwh.daoIm;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.hwh.dao.OrdersDao;
import com.hwh.vo.Orders;

import JDBCUtil.UTIL;

public class OrdersDaoImp implements OrdersDao {

	// 查询所有订单
	public ArrayList<Orders> getOrders() {

		ArrayList<Orders> orders = new ArrayList<Orders>();
		PreparedStatement pst = null;
		Connection con = UTIL.getConn();

		ResultSet rs = null;
		StringBuffer sql = new StringBuffer("select order_id,buy_id,g_id,num,all_price,o_address from orders");
		try {
			pst = con.prepareStatement(sql.toString());
			// 返回一个结果集
			rs = pst.executeQuery();
			while (rs.next()) {
				// 订单对象接收结果集的结果
				Orders or = new Orders();
				or.setOrder_id(rs.getInt("order_id"));
				or.setBuy_id(rs.getInt("buy_id"));
				or.setG_id(rs.getInt("g_id"));
				or.setNum(rs.getInt("num"));
				or.setAll_price(rs.getInt("all_price"));
				or.setO_address(rs.getString("o_address"));
				orders.add(or);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				UTIL.closeConn(rs, pst, con);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return orders;
	}

	// 查询买家订单
	public ArrayList<Orders> getBuyerOrders(int i) {

		ArrayList<Orders> orders = new ArrayList<Orders>();
		PreparedStatement pst = null;
		Connection con = UTIL.getConn();

		ResultSet rs = null;
		StringBuffer sql = new StringBuffer(
				"select order_id,buy_id,g_id,num,all_price,o_address from orders where buy_id=?");
		try {
			pst = con.prepareStatement(sql.toString());
			pst.setInt(1, i);
			// 返回一个结果集
			rs = pst.executeQuery();
			while (rs.next()) {
				// 订单对象接收结果集的结果
				Orders or = new Orders();
				or.setOrder_id(rs.getInt("order_id"));
				or.setBuy_id(rs.getInt("buy_id"));
				or.setG_id(rs.getInt("g_id"));
				or.setNum(rs.getInt("num"));
				or.setAll_price(rs.getInt("all_price"));
				or.setO_address(rs.getString("o_address"));
				orders.add(or);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				UTIL.closeConn(rs, pst, con);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return orders;
	}

	// 查询卖家订单
	public ArrayList<Orders> getSellerOrders(int i) {

		ArrayList<Orders> orders = new ArrayList<Orders>();
		PreparedStatement pst = null;
		Connection con = UTIL.getConn();

		ResultSet rs = null;
		StringBuffer sql = new StringBuffer(
				"select order_id,buy_id,g_id,num,all_price,o_address from orders where sell_id=?");
		try {
			pst = con.prepareStatement(sql.toString());
			pst.setInt(1, i);
			// 返回一个结果集
			rs = pst.executeQuery();
			while (rs.next()) {
				// 订单对象接收结果集的结果
				Orders or = new Orders();
				or.setOrder_id(rs.getInt("order_id"));
				or.setBuy_id(rs.getInt("buy_id"));
				or.setG_id(rs.getInt("g_id"));
				or.setNum(rs.getInt("num"));
				or.setAll_price(rs.getInt("all_price"));
				or.setO_address(rs.getString("o_address"));
				orders.add(or);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				UTIL.closeConn(rs, pst, con);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return orders;
	}

}
